#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
#include<QPushButton>
#include<QComboBox>
#include <QFile>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setLabelConnectedStatus(false);

    connect(ui->pushButtonConnect,&QPushButton::clicked,this,&MainWindow::onBtnConnectClicked);
    // 初始化列表数据
    itemList << "Option 1" << "Option 2" << "Option 3" << "Option 4";
    ui->comboBoxIP->addItems(itemList);
    connect(ui->comboBoxIP,QOverload<int>::of(&QComboBox::currentIndexChanged),this,&MainWindow::on_comboBox_currentIndexChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onBtnConnectClicked()
{
    // 处理按钮点击事件，例如显示一个消息框
    // QMessageBox::information(this, "Message", "Button clicked!");
    // 切换 connected 状态
    bool currentStatus = ui->labelPLCStatus->property("connected").toBool();
    setLabelConnectedStatus(!currentStatus);
}

void MainWindow::on_comboBox_currentIndexChanged(int index){
    // 获取当前选中的文本
    QString selectedText = ui->comboBoxIP->currentText();
    QMessageBox::information(this, "Selected Option", "You selected: " + selectedText);
}

void MainWindow::setLabelConnectedStatus(bool connected)
{
    ui->labelPLCStatus->setProperty("connected",connected?"true":"false");
    // 加载样式表
    QFile styleFile(":/qlabelstyle.css");
    if (styleFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream stream(&styleFile);
        QString styleSheet = stream.readAll();
        ui->labelPLCStatus ->setStyleSheet(styleSheet);
        styleFile.close();
    }
    qDebug() << "Set connected status to:" << (connected? "true" : "false");
}
